//
// SoapTrace.Net - A soap message monitoring utility. 
//
// Copyright (C) 2005 Matthew Ward
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// Matthew Ward (mrward@users.sourceforge.net)

using NUnit.Framework;
using SoapTrace.Core.Communication;
using SoapTrace.Core.Diagnostics;
using System;
using System.IO;

namespace SoapTrace.Tests.TcpPacketMonitoring
{
	/// <summary>
	/// Tests that the <see cref="TcpPacketMonitor"/> class
	/// registers a tcp connection.
	/// </summary>
	[ TestFixture ]
	public class TcpConnectionTestFixture
	{
		TcpPacketMonitor Monitor;
		int ClientConnected;
		TcpCommunicationEventArgs EventArgsReceived;
		
		/// <summary>
		/// Initialises each test.
		/// </summary>
		[ TestFixtureSetUp ]
		public void Init( )
		{
			ClientConnected = 0;
			Monitor = new TcpPacketMonitor( "209.171.52.99", 80 );
			Monitor.ClientConnected += new TcpCommunicationEventHandler( OnClientConnected );
			
			StringReader Reader = new StringReader( GetPacketXml( ) );
			PacketLogEntryCollection LogEntries = PacketLogFile.Load( Reader );
			
			foreach( PacketLogEntry Entry in LogEntries )
			{
				Monitor.ProcessPacket( Entry.Packet );
			}
		}
		
		/// <summary>
		/// Checks that the connect event was triggered.
		/// </summary>
		[ Test ]
		public void ReceivedSingleConnectionEvent( )
		{
			Assert.AreEqual( 1, ClientConnected, "Did not receive one connection event." );
		}
		
		/// <summary>
		/// Check the connection source IP address is correct.
		/// </summary>
		[ Test ]
		public void SourceIPAddress( )
		{
			Assert.AreEqual( "192.168.1.3:1692", EventArgsReceived.SourceAddress.ToString( ),
			                "Unexpected source IP address." );
		}
		
		/// <summary>
		/// Check the connection destination IP address is correct.
		/// </summary>
		[ Test ]
		public void DestinationIPAddress( )
		{
			Assert.AreEqual( "209.171.52.99:80", EventArgsReceived.DestinationAddress.ToString( ),
			                "Unexpected destination IP address." );
		}		
		
		private void OnClientConnected( object sender, TcpCommunicationEventArgs e )
		{
			++ClientConnected;
			EventArgsReceived = e;
		}
		
		/// <summary>
		/// Gets the xml to be read in by the <see cref="PacketLogFile"/>
		/// class.
		/// </summary>
		private string GetPacketXml( )
		{
			return "<Packets>\r\n" +
				"  <Packet Time=\"2005-01-31 18:49:34.77\" ServerFilter=\"209.171.52.99\" PortFilter=\"80\">\r\n" +
				"    <Bytes>00 11 F5 0B 1F 69 00 11 F5 0B 1F 6A 08 00 45 00 00 30 4B 02 40 00 80 06 E8 0B C0 A8 01 03 D1 AB 34 63 06 9C 00 50 BD AA AB 7F 00 00 00 00 70 02 FF FF 4B 4F 00 00 02 04 05 B4 01 01 04 02 </Bytes>\r\n" +
				"  </Packet>\r\n" +
				"  <Packet Time=\"2005-01-31 18:49:34.83\" ServerFilter=\"209.171.52.99\" PortFilter=\"80\">\r\n" +
				"    <Bytes>00 11 F5 0B 1F 6A 00 11 F5 0B 1F 69 08 00 45 00 00 30 1A EA 40 00 67 06 31 24 D1 AB 34 63 C0 A8 01 03 00 50 06 9C 6F C1 EF C4 BD AA AB 80 70 12 FF FF EB DF 00 00 02 04 05 8C 01 01 04 02 </Bytes>\r\n" +
				"  </Packet>\r\n" +
				"  <Packet Time=\"2005-01-31 18:49:34.88\" ServerFilter=\"209.171.52.99\" PortFilter=\"80\">\r\n" +
				"    <Bytes>00 11 F5 0B 1F 69 00 11 F5 0B 1F 6A 08 00 45 00 00 28 4B 03 40 00 80 06 E8 12 C0 A8 01 03 D1 AB 34 63 06 9C 00 50 BD AA AB 80 6F C1 EF C5 50 10 FF FF 18 7C 00 00 </Bytes>\r\n" +
				"  </Packet>\r\n" +
				"</Packets>";
		}
	}
}
